<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <!-- 默认的提交事件 -->
    <!-- <form action="http://www.baidu.com" onsubmit="return false"> -->
    <form action="http://www.baidu.com">
        账号: <input type="text" placeholder="请输入账号" id="username" name="username"><br>
        密码: <input type="text" placeholder="请输入密码" id="password" name="password"><br>
        <input type="submit">
    </form>
    <script>
        const form = document.querySelector("form");
        form.onsubmit = function(e){
            const username = document.querySelector("#username").value
            const password = document.querySelector("#password").value

            e = e || window.event
            e.preventDefault();
            //使用内置函数创建一个ajax对象
            const xhr = new XMLHttpRequest();
            //打开链接,准备发起请求
            xhr.open("POST","http://localhost:80/02.ajax_Login/login.php");
            //设置请求头
            xhr.setRequestHeader("content-type","application/x-www-form-urlencoded")
            //发送请求
            xhr.send(`username=${username}&password=${password}`);

            xhr.onload = function(){
                console.log(xhr.responseText);
            }
        }
    </script>
</body>
</html>